package com.service.lotterySystem.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.lotterySystem.dto.UserDto;
import com.service.lotterySystem.service.JoinInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.service.config.constant.Constant.*;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/11 19:18
 * @description：
 */
@RestController
@RequestMapping("JoinInfoController")
@Api(value = "活动参加人员接口", tags = "活动参加人员接口")
public class JoinInfoController {
    @Resource
    private JoinInfoService joinInfoService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 参加活动
     * @param
     * @return
     */
    @GetMapping(value = "joinActivity",produces="application/json")
    @ApiOperation(value = "参加活动")
    @ResponseBody
    public Result joinActivity(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                              @RequestParam String aId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return joinInfoService.joinActivity(userDto,aId);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }
    /**
     * 参加活动
     * @param
     * @return
     */
    @GetMapping(value = "isjoinActivityed",produces="application/json")
    @ApiOperation(value = "当前人员是否参加过活动")
    @ResponseBody
    public Result isjoinActivityed(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                               @RequestParam String aId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                boolean join = joinInfoService.isJoin(aId, userId);
                return Result.ok().setData(join);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }
    /**
     * 用户查询参加的所有活动
     * @param
     * @return
     */
    @GetMapping(value = "queryUserActivities",produces="application/json")
    @ApiOperation(value = "用户查询参加的所有活动")
    @ResponseBody
    public Result queryUserActivities(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                      @RequestParam("type") @ApiParam("type:0--历史活动（可查看中奖名单等信息）1--正在进行的活动 2--预热活动") Integer type) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                return joinInfoService.queryUserActivities(userDto,type);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * (运营)查询活动参加的所有用户以及助力排名信息
     * @param
     * @return
     */
    @GetMapping(value = "queryUsersOfActivity",produces="application/json")
    @ApiOperation(value = "查询活动参加的所有用户以及助力排名信息（不需要token）")
    @ResponseBody
    public Result queryUsersOfActivity(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                       @RequestParam("aid")@ApiParam("aid") String aid,
                                        @RequestParam("isMe")@ApiParam("isMe") Boolean isMe) throws Exception {
        //if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            //String userId = UserTokenManager.getUserId(cloudAuthToken);
           // if (redisUtil.exist(cloudAuthToken+userId)) {
                return joinInfoService.queryUsersOfActivity(aid,isMe,cloudAuthToken);
            //}
           // return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        //}
        //return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    @ApiOperation(value = "导出参与用户清单")
    @RequestMapping(value ="/exportJoinUsers",method = RequestMethod.GET)
    @ResponseBody
    public Result exportPrizeList(@RequestParam("aid")@ApiParam("aid") String aid) throws Exception {
        return joinInfoService.exportJoinUsers(aid);
    }

    /**
     * 退出活动
     * @param
     * @return
     */
    @GetMapping(value = "exitActivity",produces="application/json")
    @ApiOperation(value = "退出活动")
    @ResponseBody
    public Result exitActivity(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                   @RequestParam @ApiParam("活动id") String aid) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return joinInfoService.deleteById(userId,aid);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }

}
